package com.liu.project.controller;


import com.liu.project.pojo.LoginUser;
import com.liu.project.pojo.User;
import com.liu.project.pojo.UserCollection;
import com.liu.project.service.UserCollectionService;
import com.liu.project.service.UserService;
import com.liu.project.utils.Message;
import com.liu.project.utils.UserUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: project
 * @description: 证书收藏操作
 * @author: 86187
 * @create: 2022-03-02 11:09
 **/

@Api(tags = "收藏操作",description = "证书收藏的展示、增加、删除")
@RestController
@RequestMapping("/api/collection")
public class CollectionController {

    @Autowired
    UserService userService;

    @Resource
    UserCollectionService userCollectionService;

    @ApiOperation("获当前用户的收藏")
    @ApiResponses ({@ApiResponse(code = 200,message = "请求成功",response = Message.class),
                    @ApiResponse(code = 0,message = "你还没有收藏哦！")})
    @GetMapping("/find")
    public Message<List<UserCollection>> finAllByUid() {
        LoginUser loginUser = UserUtil.getLoginUser();
        String username = loginUser.getUsername();
        User userByUserName = userService.findUserByUserName(username);
        List<UserCollection> allByUid = userCollectionService.findAllByUid(userByUserName.getId());
        if (allByUid!=null){
            return new  Message<List<UserCollection>>(200,"请求成功", allByUid);
        }
          else return new Message(0,"你还没有收藏哦！");
    }

    @ApiOperation("添加收藏")
    @ApiResponses ({@ApiResponse(code = 200,message = "请求成功",response = Message.class),
            @ApiResponse(code = 400,message = "请求失败")})
    @ApiImplicitParam(name = "certificateId",value = "收藏的证书id")
    @PostMapping("/add/{certificateid}")
    public Message addCollection(@PathVariable("certificateid") int certificateid){
        LoginUser loginUser = UserUtil.getLoginUser();

        String username = loginUser.getUsername();
        User userByUserName = userService.findUserByUserName(username);

        if (userCollectionService.insertCollection(userByUserName.getId(),certificateid)){
            return new Message(200,"请求成功");
        }else return new Message(400,"请求失败");
    }

    @ApiOperation("删除收藏记录")
    @ApiImplicitParam(name = "id",value = "收藏id")
    @ApiResponses ({@ApiResponse(code = 200,message = "请求成功"),
            @ApiResponse(code = 400,message = "请求失败")})
    @GetMapping("/delete/{certificateid}")
    public Message deleteCollection(@PathVariable("certificateid") int certificateid){
        if (userCollectionService.deleteCollection(certificateid)){
            return new Message(200,"请求成功");
        }else return new Message(400,"请求失败");
    }




}
